package com.itbaizhan.shopping_cartgoods_service.listen;


import com.itbaizhan.shopping_common.pojo.CartGoods;
import com.itbaizhan.shopping_common.pojo.DomainEvent;
import com.itbaizhan.shopping_common.service.CartGoodsService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;

@RocketMQMessageListener(
        topic = "cart_event_topic",
        consumerGroup = "cart_goods_group"
)
public class CartGoodsListener implements RocketMQListener<Object> {
    @DubboReference
    private CartGoodsService cartGoodsService;

    @Override
    public void onMessage(Object o) {
        DomainEvent event = (DomainEvent) o;
        // 判断消息类型
        if(event.getEventType().equals("update_cart")) { //更新商品
            cartGoodsService.refreshCartGoods((CartGoods) event.getData());
        }else if(event.getEventType().equals("del_cart")){ //下架商品 ,或者创建订单
            String s = (String) event.getData();
            if(!s.contains("#")) { //下架商品
                cartGoodsService.deleteCartGoods(Long.valueOf(s));
            }else{ // 删除该用户拥有的购物车数据
                String[] split = s.split("#");
                cartGoodsService.deleteCartGoods(Long.valueOf(split[0]), Long.valueOf(split[1]));
            }
        }
    }
}
